import { Component, OnInit } from '@angular/core';
import {Globals} from '../../../../globals';
import {PageHelper} from '../../../../utils/page-helper-model';
import {ApiUrl} from '../../../../config/api-url';
import {TreeNodeInterface} from '../../../../shared/interface/TreeNodeInterface';
import {BaseService} from '../../../../shared/service/base.service';
import {FormBuilder, FormGroup} from '@angular/forms';

@Component({
  selector: 'app-module-list',
  templateUrl: './module-list.component.html',
  styleUrls: ['./module-list.component.less']
})
export class ModuleListComponent implements OnInit {
  // 查看运行当前设备
  isMobile = Globals.isMobile();
  listOfMapData = [];
  mapOfExpandedData: { [key: string]: TreeNodeInterface[] } = {};
  // 显示模态框
  isVisible = false;
  // 加载动画
  isOkLoading: boolean;
  // 分页
  pageHelper: PageHelper = new PageHelper;
  formModel: FormGroup;
  module: any;
  constructor(private baseService: BaseService,private fb: FormBuilder) { }

  ngOnInit(): void {
    this.buildForm();
    this.findList();
  }
  buildForm() {
    this.formModel = this.fb.group({
      status: [null,[]],
      type: [null,[]],
      keyword: [null, []],
    });
  }
  // 获取列表
  findList() {
    this.baseService.findListByMuchCondition(ApiUrl.ModuleUrl + '/findAllocationModuleList',this.formModel.value, this.pageHelper).subscribe(
      data => {
        if (data.status == 0) {
          this.pageHelper.totalItems = data.data.total;
          this.listOfMapData = data.data.list;
          if (this.listOfMapData.length > 0) {
            this.listOfMapData.forEach(item => {
              item['children'] = item.childList;
              this.mapOfExpandedData[item.key] = this.baseService.zorro.convertTreeToList(item);
            });
          }
        } else {
          this.baseService.layer.messageError(data.msg);
        }
      }
    );
  }
  nzPageIndexChange($event: number) {
    this.pageHelper.currentPage = $event;
    this.findList();
  }

  submitForm(){
    this.pageHelper.currentPage = 1;
    this.findList();
  }

  updateStatus(data,e){
    console.log(e)
    let param = {
      status: e,
      id: data.id
    }
    this.baseService.put(ApiUrl.ModuleUrl + "/updateStatus",param).subscribe(res => {
      if(res.status == 0){
        this.baseService.layer.messageSuccess("修改成功");
      }else{
        this.baseService.layer.messageError(res.msg);
      }
    })
  }
  editModal(data){
    console.log(data)
    this.isVisible = true;
    if(data != null){
      this.module = JSON.parse(JSON.stringify(data));
    }
  }
  childEditModel(data){
    this.module = JSON.parse(JSON.stringify(data));
    this.module['parentId'] =  this.module.id;
    this.module['editType'] = "insert"
    this.editModal(this.module);
  }
  handleCancel(){
    this.isVisible = false;
  }

  del(item){
    this.baseService.layer.modalDeleteConfirm('确定要删除"'+item.name+'"模块吗？前端模块功能可能会发生变化!', confirm => {
      if (confirm) {
        this.baseService.layer.modalDeleteConfirm('(第二次确认)确定要删除"'+item.name+'"模块吗', confirm => {
          if (confirm) {
            this.baseService.delete(ApiUrl.ModuleUrl + '/delete', item.id).subscribe(
              data => {
                if (data.status == 0) {
                  this.baseService.layer.messageSuccess("删除成功");
                  this.findList();
                } else {
                  this.baseService.layer.messageError(data.msg);
                }
              }
            );
          }
        });
      }
    });
  }
}
